//订单栏目
var OrderNavCtrl = ['$scope', '$rootScope','ThirdDev', function ($scope, $rootScope,ThirdDev) {
        $scope.menu_list = [
            {name: $rootScope.language.TIP_ORDER_RECEIPT, type: 1, show: 1},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_NORMAL, url: "#/order/xiaopiao_list/main", id: "xiaopiao_list_main", type: 2, show: $rootScope.purview['\/normal'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_RETURN, url: "#/order/xiaopiao_list/return", id: "xiaopiao_list_return", type: 2, show: $rootScope.purview['\/return'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_UNPAY, url: "#/order/xiaopiao_list/uncheck", id: "xiaopiao_list_uncheck", type: 2, show: $rootScope.purview['\/unpaid'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_RETURNALBE, url: "#/order/xiaopiao_list/returnable", id: "xiaopiao_list_returnable", type: 2, show: $rootScope.purview['\/returnable'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_OFFLINE, url: "#/order/xiaopiao_list_local/offline", id: "xiaopiao_list_offline", type: 2, show: $rootScope.purview['\/offline'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_NOT_UPLOAD, url: "#/order/xiaopiao_list_local/not_upload", id: "xiaopiao_list_not_upload", type: 2, show: $rootScope.purview['\/not_upload'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_LACK, url: "", id: "", type: 2, show: $rootScope.purview['\/lack_goods'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_ONLINE, type: 1, show: $rootScope.purview['\/online_receipt'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_WAIT, url: "", id: "", type: 2, show: $rootScope.purview['\/wait_receipt'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_ARRIVE, url: "", id: "", type: 2, show: $rootScope.purview['\/arrival_receipt'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_DAY_ARRIVE, url: "", id: "", type: 2, show: $rootScope.purview['\/day_arrival_receipt'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_SERVICE, type: 1, show: $rootScope.purview['\/service_receipt'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_CHECK, url: "", id: "", type: 2, show: $rootScope.purview['\/check_receipt'].show},
            {name: $rootScope.language.TIP_ORDER_RECEIPT_FILED, url: "", id: "", type: 2, show: $rootScope.purview['\/filed_receipt'].show},
            {name: $rootScope.language.TIPS_CASHIER_DEPOSIT_RECORD, url: "#/order/service/depositRecord_list", id: "DepositRecord_list", type: 2, show:!ThirdDev.is_lqx() },  //$rootScope.purview['\/depositRecord'].show
            {name: $rootScope.language.TIPS_CASHIER_DEPOSITRETURN_RECORD, url: "#/order/service/depositReturnRecord_list", id: "DepositReturnRecord_list", type: 2, show: !ThirdDev.is_lqx()},  //$rootScope.purview['\/depositReturnRecord'].show
            {name: $rootScope.language.TIP_MAINTENANCE_RECORD, url: "#/order/fix_list/fix", id: "service_list_fix", type: 2, show: $rootScope.purview['\/CashierIndexCtrl\/fix'].show}  
        ];
    }];
//小票部分 #####################################################################
/**
 * 小票列表页控制器
 * @param {type} $routeParams
 * @returns {undefined}
 */
var OrderXiaopiaoListCtrl = ['$q', '$scope', '$rootScope', '$location','apiService','dbUpdate', 'timeStamp2String', '$routeParams', 'ngDialog', 'ReceiptRecord', 'ShopSetCheck', 'Print', 'ShopSearch', 'dbGetOne', 'checkGetList','commonInterface','PosUser', function ($q,$scope, $rootScope, $location, apiService,dbUpdate,timeStamp2String, $routeParams, ngDialog, ReceiptRecord, ShopSetCheck, Print, ShopSearch,dbGetOne, checkGetList,commonInterface,PosUser) {
        ShopSetCheck();
        $scope.buttonFilter = function () {
            switch ($routeParams.cate) {
                case 'main':
                    $scope.curr = 'xiaopiao_list_main';
                    $scope.buttonClose = '0';
                    $scope.buttonPrint = '0';
                    $scope.buttonReturn = '0';
                    $scope.buttonThirdReturn = '0';
                    $scope.buttonThirdUpload = '0';
                    $scope.buttonUpload = '0';
                    $scope.buttonDestroy = '0';
                    $scope.buttonMore = '1';
                    $scope.buttonHigh = $rootScope.purview['\/OrderXiaopiaoListCtrl\/search:normal'].show;
                    $scope.buttonRefresh = '1';
                    break;
                case 'return':
                    $scope.curr = 'xiaopiao_list_return';
                    $scope.buttonClose = '0';
                    $scope.buttonPrint = '0';
                    $scope.buttonReturn = '0';
                    $scope.buttonThirdReturn = '0';
                    $scope.buttonThirdUpload = '0';
                    $scope.buttonUpload = '0';
                    $scope.buttonDestroy = '0';
                    $scope.buttonMore = '1';
                    $scope.buttonHigh = $rootScope.purview['\/OrderXiaopiaoListCtrl\/search:return'].show;
                    $scope.buttonRefresh = '1';
                    break;
                case 'uncheck':
                    $scope.curr = 'xiaopiao_list_uncheck';
                    $scope.buttonClose = '0';
                    $scope.buttonPrint = '0';
                    $scope.buttonReturn = '0';
                    $scope.buttonThirdReturn = '0';
                    $scope.buttonThirdUpload = '0';
                    $scope.buttonUpload = '0';
                    $scope.buttonDestroy = '0';
                    $scope.buttonMore = '1';
                    $scope.buttonHigh = $rootScope.purview['\/OrderXiaopiaoListCtrl\/search:unpaid'].show;
                    $scope.buttonRefresh = '1';
                    break;
                case 'returnable':
                    $scope.curr = 'xiaopiao_list_returnable';
                    $scope.buttonClose = '0';
                    $scope.buttonPrint = '0';
                    $scope.buttonReturn = '0';
                    $scope.buttonThirdReturn = '0';
                    $scope.buttonThirdUpload = '0';
                    $scope.buttonUpload = '0';
                    $scope.buttonDestroy = '0';
                    $scope.buttonMore = '1';
                    $scope.buttonHigh = $rootScope.purview['\/OrderXiaopiaoListCtrl\/search:returnable'].show;
                    $scope.buttonRefresh = '1';
                    break;
            }
        };
        $scope.buttonFilter();
        //$routeParams.cate;
        //main:正常小票 return:退单小票 uncheck:挂单和未支付 returnable:可退 not_upload:未上传
        $scope.xiaopiao_list = [];
//    $scope.detail_switch = 0;
        //获取前一天数据
        $scope.scoll = {}; //滚动加载器
        $scope.scoll.busy = false;
        //定义当前时间
        var newTime = new Date();
        var middleTime = newTime;
        $scope.scoll.nextpage = function () {
            if ($scope.scoll.busy) {
                return;
            }
            //baison_app.api.toast({msg: '异步加载...'});
            $scope.scoll.busy = true;
            var filter = $scope.filter;
            if ($scope.xiaopiao_list.length > 0) {
                filter.day = $scope.xiaopiao_list[$scope.xiaopiao_list.length - 1]['key'];
            }
            filter.is_pre = 1;
            ReceiptRecord.get_day(filter, $routeParams.cate).success(function (response) {
                if (response.status == 1) {
                    for (i in response.data) {
                        $scope.xiaopiao_list.push({key: i, value: response.data[i]});
                    }
                } else {
                    baison_app.api.toast({msg: $rootScope.language.TIP_ORDER_RECEIPT_DATA});
                }
                $scope.scoll.busy = false;
            });
        };
        //刷新本页数据
        $scope.refresh_data = function () {
            $scope.filter = {}; //查询条件
            $scope.xiaopiao_list = [];
            $scope.filter.day = '';
            ReceiptRecord.get_day($scope.filter, $routeParams.cate).success(function (response) {
                if (response.status == 1) {
                    if (response.data != '') {
                        var data = [];
                        for (i in response.data) {
                            data.push({key: i, value: response.data[i]});
                        }
                        $scope.xiaopiao_list = data;
                    } else {
                        $scope.scoll.nextpage();
                    }
                }
                $scope.scoll.busy = false;
            });
        };
        $scope.refresh_data();
        //打开高级查询窗口
        var window_id = 0;
        $scope.search = function () {
            $scope.filter = {};
            if (typeof $scope.filter.record_time_start === 'string') {
                $scope.filter.record_time_start = new Date($scope.filter.record_time_start);
            }
            if (typeof $scope.filter.record_time_end === 'string') {
                $scope.filter.record_time_end = new Date($scope.filter.record_time_end);
            }
            window_id = ngDialog.open({closeByDocument: false,
                template: './html/order/xiaopiao/xiaopiao_select.html',
                disableAnimation: true,
                scope: $scope,
//                closeByDocument: false
                trapFocus: false,
                controller: ['$scope', function ($scope) {
                        $scope.vip_show = true;
                        if ($routeParams.cate == 'offline' || $routeParams.cate == 'not_upload') {
                            $scope.vip_show = false;
                        }
                    }]
            });
        };
        $scope.search_data = function (filter) {
            if ($scope.filter.record_time_start == '' || $scope.filter.record_time_end == '' ||
                    typeof $scope.filter.record_time_start == 'undefined' ||
                    typeof $scope.filter.record_time_end == 'undefined') {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIPS_CUNHUO_MESSAGE_TIME});
                return;
            }
            if ($scope.filter.record_time_start > $scope.filter.record_time_end) {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIPS_CUNHUO_MESSAGE_TIME_LIMIT});
                return;
            }
            $scope.filter = filter;
            ReceiptRecord.get_list($scope.filter, $routeParams.cate).success(function (response) {
                $scope.xiaopiao_list = [];
                if (response.status == 1 && response.data != '') {
                    var data = [];
                    for (i in response.data) {
                        data.push({key: i, value: response.data[i]});
                    }
                    $scope.xiaopiao_list = data;
                }
                $scope.scoll.busy = false;
            });
            $scope.filter = {};
            ngDialog.close(window_id);
        };
        //重置查询状态
        $scope.clear = function () {
            $scope.filter = {};
        };
        
        
        
        //打开详情页面
        $scope.show_detail = function (record_code) {

//        $scope.detail_switch = 1;
            ReceiptRecord.get_one(record_code).success(function (response) {

//                baison_app.api.alert({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON],msg: response})

                ShopSearch(response.data.record.shop_code).then(function (n) {
                    n.forEach(function (v) {
                        if (v.ShopCode == response.data.record.shop_code) {
                            response.data.record.ShopName = v.ShopName;
                        }
                    });
                });

                $.each(response.data.payment, function (i, v) {

                    var remark = v.remark.split("||");
                    response.data.payment[i].reason = remark[1];
                    response.data.payment[i].remark = remark[0];
                    checkGetList.get_name_by_code(v.trade_type_code).then(function (ret) {
                        response.data.payment[i].trade_type_name = ret[0].CheckName;
                    });

                });

                /*added by lqx maowei on 20170422 当已退款coupon_code后三位***/
                response.data.payment.forEach(function (n) {
                    if(response.data.record.is_return == 1){
                        n.coupon_code = n.coupon_code.substr(0,3)+'***';
                    }
                });


                $scope.xiaopiao = response.data;
                //console.log($scope.xiaopiao);
                $scope.xiaopiao.record.guide_user_name = commonInterface.get_guides_user_name($scope.xiaopiao.record_detail);
                //console.log(JSON.stringify($scope.xiaopiao));
                PosUser.get_user_by_code($scope.xiaopiao.record.trade_user_code).then(function (ret) {
                   $scope.xiaopiao.record.trade_user_name = ret.UserName;
                   //console.log($scope.xiaopiao.record);
                });
                
                $scope.xiaopiao.record.deviceId = $scope.xiaopiao.record.device_id;
                $scope.xiaopiao.record.remark = $scope.xiaopiao.record.remark;
                //baison_app.api.alert({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON],msg:$scope.xiaopiao,title:'$scope.xiaopiao'});
                $scope.xiaopiao.record.paymoney = $scope.xiaopiao.record.pay_money > $scope.xiaopiao.record.final_money ? $scope.xiaopiao.record.final_money : $scope.xiaopiao.record.pay_money;
//                baison_app.api.alert({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON],msg: $scope.xiaopiao, title: 222});
                //console.log($scope.xiaopiao);
                if($scope.xiaopiao.record.is_settlement == 2){
                    $scope.xiaopiao.record.is_checkout = 2;
                }
                switch ($routeParams.cate) {
                    case 'main':
                        $scope.buttonClose = '1';
                        $scope.buttonPrint = $rootScope.purview['\/OrderXiaopiaoListCtrl\/to_print:normal'].show;
                        $scope.buttonReturn = $rootScope.purview['\/CashierReturnCtrl\/no_return_receipt'].show;
                        $scope.buttonThirdReturn = '0';
                        $scope.buttonThirdUpload = '0';
                        $scope.buttonUpload = '0';
                        $scope.buttonDestroy = '0';
                        $scope.buttonHigh = '0';
                        $scope.buttonRefresh = '0';
                        $scope.coupon_display = '1';
                        break;
                    case 'return':
                        $scope.buttonClose = '1';
                        $scope.buttonPrint = $rootScope.purview['\/OrderXiaopiaoListCtrl\/to_print:return'].show;
                        $scope.buttonReturn = '0';
                        $scope.buttonThirdReturn = $scope.xiaopiao.record.is_checkout==2;
                        $scope.buttonUpload = '0';
                        $scope.buttonDestroy = '0';
                        $scope.buttonHigh = '0';
                        $scope.buttonRefresh = '0';
                        $scope.coupon_display = '1';
                        break;
                    case 'uncheck':
                        $scope.buttonClose = '1';
                        $scope.buttonPrint = '0';
                        $scope.buttonReturn = '0';
                        $scope.buttonThirdReturn = '0';
                        $scope.buttonThirdUpload = '0';
                        $scope.buttonUpload = '0';
                        $scope.buttonDestroy = $rootScope.purview['\/OrderXiaopiaoListCtrl\/selectDestroy:unpaid'].show;
                        $scope.buttonHigh = '0';
                        $scope.buttonRefresh = '0';
                        $scope.coupon_display = '1';
                        //根据 小票单号查询 第三方支付信息
                        third_pay_query();
                        break;
                    case 'returnable':
                        $scope.buttonClose = '1';
                        $scope.buttonPrint = $rootScope.purview['\/OrderXiaopiaoListCtrl\/to_print:returnable'].show;
                        $scope.buttonReturn =  $rootScope.purview['\/CashierReturnCtrl\/no_return_receipt'].show;
                        $scope.buttonThirdReturn = '0';
                        $scope.buttonThirdUpload = '0';
                        $scope.buttonUpload = '0';
                        $scope.buttonDestroy = '0';
                        $scope.buttonHigh = '0';
                        $scope.buttonRefresh = '0';
                        $scope.coupon_display = '1';
                        break;
                }
            });



        };
        $scope.close_detail = function () {
//        $scope.detail_switch = 0;
            $scope.buttonFilter();
        };
        //前往退货页面
        $scope.goto_return = function () {
            var configData = $api.getStorage('configData');
            if ($rootScope.purview['\/CashierIndexCtrl\/end_sale'].show == 1) {
                if (configData.finished == 1) {
                    baison_app.api.toast({msg:$rootScope.language.TIP_INDEX_FINISH_WARNING});
                    return;
                }
            }
            if ($scope.xiaopiao.record.record_code != '') {
                $location.path('/cashier/return/' + $scope.xiaopiao.record.record_code);
            }
        };
        //第三方支付退款
        $scope.goto_third_return = function(){
            ReceiptRecord.third_pay_return({'record_code':$scope.xiaopiao.record.record_code}).then(function(){
                baison_app.api.toast({msg:$rootScope.language.TIPS_OPTION_SUCCESS});
                $scope.buttonThirdReturn = '0';
                return;
            });
        };
        //获取支付信息
        var get_third_pay = function(data){
            var pid = $scope.xiaopiao.record_detail[0].pid;
            dpayment = [];
            switch(data.type){
                case '1':{ //微信
                    var info = {
                        'coupon_code':data.transaction_id,
                        'lastchanged':timeStamp2String(0,'datetime'),
                        'money':data.cash_fee/100,
                        'pid':pid,
                        'remark':'交易流水号:'+data.transaction_id,
                        'trade_type_code':data.trade_type == "MICROPAY"?'010':'008',
                        'trade_type_name':data.trade_type == "MICROPAY"?'微信刷卡':'微信扫码'
                    };
                    $scope.xiaopiao.payment.push(info);
                    dpayment.push(info);
                    $scope.xiaopiao.record.pay_money = $scope.xiaopiao.record.paymoney+data.cash_fee/100*1;
                    break;
                }
                case '2':{ //支付宝
                    var info = {
                        'coupon_code':data.trade_no,
                        'lastchanged':timeStamp2String(0,'datetime'),
                        'money':data.total_amount,
                        'pid':pid,
                        'remark':'交易流水号:'+data.trade_no,
                        'trade_type_code':'011',
                        'trade_type_name':'支付宝'
                    };
                    $scope.xiaopiao.payment.push(info);
                    dpayment.push(info);
                    $scope.xiaopiao.record.pay_money = $scope.xiaopiao.record.paymoney+data.total_amount*1;
                    break;
                }
            }
        };
        //查询第三方支付信息
        var third_pay_query = function(){
            var len = $scope.xiaopiao.record_detail.length;
            if(len <= 0){
                return;
            }
            ReceiptRecord.third_pay_query({'record_code':$scope.xiaopiao.record.record_code}).then(function(payment){
                if(payment.data.status == 1){ //获取成功
                    $scope.buttonThirdUpload = 1;
                    switch(payment.data.data.type){
                        case '1':{ //微信
                            get_third_pay(payment.data.data);
                            break;
                        }
                        case '2':{ //支付宝
                            get_third_pay(payment.data.data);
                            break;
                        }
                    }
                } 
            });
        };
        
        //检查是否可以上传小票
        var check_record_upload = function(){
            
            var paymoney = 0;
            $scope.xiaopiao.payment.forEach(function(e){
                paymoney += e.money*1;
            });
            if(paymoney != $scope.xiaopiao.record.final_money){
                return false;
            }
            return true;
        };
        
        //第三方支付 查询到支付信息 上传
        $scope.goto_third_upload = function(){
            if($scope.buttonThirdUpload == '2'){
                return;
            }
            var record_code = $scope.xiaopiao.record.record_code;
            $scope.xiaopiao.record_code = record_code;
            $scope.xiaopiao.record.pay_money = $scope.xiaopiao.record.final_money;
            var data = {};
            data.record_code = record_code;
            data.promotion = $scope.xiaopiao.promotion;
            data.record = $scope.xiaopiao.record;
            data.record_detail = $scope.xiaopiao.record_detail;
            data.payment = dpayment;
            if(check_record_upload()){
                $scope.xiaopiao.record.is_checkout = 1;
                $scope.buttonThirdUpload = '2'; //处理中
                apiService('pos/pos/update', data).then(function (success,err) {
                    //console.log(success);
                    //console.log(err);
                    if (success.data.status == 1) {
                        baison_app.api.toast({msg:$rootScope.language.TIPS_OPTION_SUCCESS});
                        //更新本地状态为已同步
                        dbUpdate('PosReceiptRecord', {'is_upload': 1,'is_checkout':1}, {record_code: record_code});
                        $scope.buttonThirdUpload = '0';
                    }else{
                        baison_app.api.toast({msg:success.data.message});
                    }
                    
                });
            }else{
                baison_app.api.toast({msg:$rootScope.language.TIP_NO_PAYED});
            }
            
        };
        //暂时写在这里
        //补印小票
        $scope.to_print = function () {
            //正式版
            
            //获取小票商品明细信息
            var get_goods_detail_infos = function(record_detail){
                var deferred = $q.defer();
                var promise = deferred.promise;
                record_detail.forEach(function(n,i){
                    deferred[i] = $q.defer();
                    promise[i] = deferred[i].promise;
                    dbGetOne('select weight,unit_name from PosGoods where GoodsCode="' + n.goods_code + '" order by CategoryCode desc, SortCode desc').then(function(ret){
                        n.weight = ret.weight;
                        n.unit_name = ret.unit_name;
                        deferred[i].resolve('success');
                    });
                   
                });
                return $q.all(promise);
            };
            
            //获取小票信息
            ReceiptRecord.get_one($scope.xiaopiao.record.record_code).then(function (ret) {
                var record = {};
                var configData = $api.getStorage('configData');
                var printData = configData.printData;
                var shop_addr = '';
                var shop_tel = '';
                var web_site = '';
                var shop_name = '';
                if (printData != undefined) {
                    shop_addr = printData['addr'];
                    shop_tel = printData['tel'];
                    web_site = printData['web_site'];
                }
                ShopSearch(ret.data.data.record.shop_code).then(function (n) {

                    shop_name = n[0].ShopName;
                    record.record_code = ret.data.data.record.record_code;
                    record.record_time = ret.data.data.record.record_time;
                    record.vip_code = ret.data.data.record.vip_code;
                    record.pay_money = ret.data.data.record.final_money;
                    record.total_num = ret.data.data.record.num;
                    record.total_money = ret.data.data.record.money;
                    record.shop_name = shop_name;
                    record.user_code = ret.data.data.record.trade_user_code; //收银员
                    record.user_name = ret.data.data.record.trade_user_name; 
                    record.guide_user_name = commonInterface.get_guides_user_name(ret.data.data.record_detail);//导购员
                    record.brand_name = $rootScope.language.TIP_ORDER_RECEIPT_BRAND_NAME;
                    record.shop_addr = shop_addr;
                    record.shop_tel = shop_tel;
                    record.web_site = web_site;
                    record.record_detail = ret.data.data.record_detail;
                    record.payment = ret.data.data.payment;
                    record.promotion = ret.data.data.promotion;
                    get_goods_detail_infos(record.record_detail).then(function(){
                        Print.xp_print($rootScope.language.TIP_ORDER_RECEIPT_BRAND_PRINT, record);
                    });
                    
                });
            });

        };
        //作废小票
        $scope.selectDestroy = function () {
            //更新服务器小票主表字段is_cancel为1
            ReceiptRecord.receipt_cancel({'record_code': $scope.xiaopiao.record.record_code}).then(function (ret) {
                if (ret.data.status == 1) { //作废成功
                    //删除本地小票主表与详细表里的对应数据
                    ReceiptRecord.receipt_cancel_local({'record_code': $scope.xiaopiao.record.record_code}).then(function (ret) {
                        $scope.close_detail();
                        $scope.refresh_data();
                        baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_ORDER_RECEIPT_DESTROY});
                    });
                }
            });
        };
    }];

var OrderXiaopiaoDetailCtrl = ['$scope', '$routeParams', 'ReceiptRecord', function ($scope, $routeParams, ReceiptRecord) {
        var record_code = $routeParams.record_code;
    }];

//离线小票部分数据
var OrderXiaopiaoListLocalCtrl = ['$q', '$scope', '$rootScope', '$location', 'timeStamp2String', '$routeParams', 'ngDialog', 'BaseGoods', 'ReceiptRecord', 'ShopSetCheck', 'Print', 'waitUpLoad',  'ShopSearch', 'checkGetList','dbGetOne', 'apiService','dbUpdate','commonInterface',function ($q,$scope, $rootScope, $location, timeStamp2String, $routeParams, ngDialog, BaseGoods, ReceiptRecord, ShopSetCheck, Print, waitUpLoad, ShopSearch, checkGetList,dbGetOne,apiService,dbUpdate,commonInterface) {
        ShopSetCheck();
        //刷新本页数据
        $scope.filter = {}; //查询条件
        $scope.refresh_data = function () {
            if ($routeParams.cate == 'not_upload') {
                $scope.xiaopiao_list = [];
                ReceiptRecord.get_list_local_not_upload().then(function (data) {
                    $scope.xiaopiao_list.push({key: '1', value: data});
                });
            } else if ($routeParams.cate == 'offline') {
                var nowTime = new Date();
                var i = timeStamp2String(nowTime, 'date');
                ReceiptRecord.get_list_local_offline({day: i, is_pre: 0}).then(function (data) {
                    $scope.xiaopiao_list = [];
                    if (typeof data[0] == "undefined") {
                        ReceiptRecord.get_list_local_offline({day: i, is_pre: 1}).then(function (data) {
                            if (data.length == 0) {
                                baison_app.api.toast({msg: $rootScope.language.TIP_ORDER_RECEIPT_DATA});
                            } else {
                                var dayTime = data[0]['record_time'].slice(0, 10);
                                $scope.xiaopiao_list.push({key: dayTime, value: data});
                            }
                        });
                    } else {
                        $scope.xiaopiao_list.push({key: i, value: data});
                    }
                });
            }
        };
        $scope.refresh_data();
        $scope.buttonFilter = function () {
            switch ($routeParams.cate) {
                case 'offline':
                    $scope.curr = 'xiaopiao_list_offline';
                    $scope.buttonClose = '0';
                    $scope.buttonPrint = '0';
                    $scope.buttonReturn = '0';
                    $scope.buttonUpload = '0';
                    $scope.buttonDestroy = '0';
                    $scope.buttonMore = '1';
                    $scope.buttonHigh = $rootScope.purview['\/OrderXiaopiaoListLocalCtrl\/search:offline'].show;
                    $scope.buttonRefresh = '1';
                    $scope.buttonThirdReturn = '0';
                    $scope.buttonThirdUpload = '0';
                    
                    break;
                case 'not_upload':
                    $scope.curr = 'xiaopiao_list_not_upload';
                    $scope.buttonClose = '0';
                    $scope.buttonPrint = '0';
                    $scope.buttonReturn = '0';
                    $scope.buttonUpload = '0';
                    $scope.buttonDestroy = '0';
                    $scope.buttonMore = '0';
                    $scope.buttonHigh = $rootScope.purview['\/OrderXiaopiaoListLocalCtrl\/search:not_upload'].show;
                    $scope.buttonRefresh = '1';
                    $scope.buttonThirdReturn = '0';
                    $scope.buttonThirdUpload = '0';
                    break;
            }
        };
        $scope.buttonFilter();
        $scope.xiaopiao_list = [];
//    $scope.detail_switch = 0;
        //获取前一天数据
        $scope.scoll = {}; //滚动加载器
        $scope.scoll.busy = false;
        //定义当前时间
        var newTime = new Date();
        var middleTime = newTime;
        $scope.scoll.nextpage = function () {
            var i = timeStamp2String(middleTime, 'date');
            ReceiptRecord.get_list_local_offline({day: i, is_pre: 1}).then(function (data) {
//                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON],title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language. data, title: '更多'});
                if (data.length == 0) {
                    baison_app.api.toast({msg: $rootScope.language.TIP_ORDER_RECEIPT_DATA});
                } else {
                    var dayTime = data[0]['record_time'].slice(0, 10);
                    $scope.xiaopiao_list.push({key: dayTime, value: data});
                    middleTime = data[0]['record_time'];
                }
            });
        };
        //打开高级查询窗口
        var window_id = 0;
        $scope.search = function () {
//            $scope.filter = {};
//            if (typeof $scope.filter.record_time_start === 'string') {
//                $scope.filter.record_time_start = new Date($scope.filter.record_time_start);
//            }
//            if (typeof $scope.filter.record_time_end === 'string') {
//                $scope.filter.record_time_end = new Date($scope.filter.record_time_end);
//            }
            window_id = ngDialog.open({closeByDocument: false,
                template: './html/order/xiaopiao/xiaopiao_select.html',
                disableAnimation: true,
                scope: $scope,
//                closeByDocument: false
                trapFocus: false
            });
        };
//高级查询搜索结果
        $scope.search_data = function () {
            if ($scope.filter.record_time_start == '' ||
                    $scope.filter.record_time_end == '' ||
                    typeof $scope.filter.record_time_start == 'undefined' ||
                    typeof $scope.filter.record_time_end == 'undefined') {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIPS_CUNHUO_MESSAGE_TIME});
                return;
            }
            if ($scope.filter.record_time_start > $scope.filter.record_time_end) {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIPS_CUNHUO_MESSAGE_TIME_LIMIT});
                return;
            }
            $scope.filter.record_time_start = timeStamp2String($scope.filter.record_time_start, 'datetime');
            $scope.filter.record_time_end = timeStamp2String($scope.filter.record_time_end, 'date');
            $scope.filter.record_time_end = $scope.filter.record_time_end.concat(" ");
            $scope.filter.record_time_end = $scope.filter.record_time_end.concat("23:59:59");
            $scope.filter.record_time_end = timeStamp2String($scope.filter.record_time_end, 'datetime');
            var time_index = '';
            ReceiptRecord.get_list_local_offline($scope.filter, $routeParams.cate).then(function (response) {
                if (response.length == 0) {
                    baison_app.api.toast({msg: $rootScope.language.TIPS_CUNHUO_SEARCH_GOODS});
                } else {
                    $scope.xiaopiao_list = [];
                    var xiaopiaoData = [];
                    var xp_offline_list = {};
                    if (response != undefined && response != null) {
                        time_index = response[0].record_time.slice(0, 10).toString();
                    }
                    $.each(response, function (index, info) {
                        var info_time = info.record_time.slice(0, 10).toString();
                        if (time_index == info_time) {
                            if (typeof xp_offline_list[time_index] != 'object') {
                                xp_offline_list[time_index] = [];
                            }
                            xp_offline_list[time_index].push(info);
                        } else {
                            time_index = info.record_time.slice(0, 10).toString();
                            if (typeof xp_offline_list[time_index] != 'object') {
                                xp_offline_list[time_index] = [];
                            }
                            xp_offline_list[time_index].push(info);
                        }
                    });
                    $.each(xp_offline_list, function (index, info) {
                        var key = info[0].record_time.slice(0, 10);
                        $scope.xiaopiao_list.push({key: key, value: info});
                    });
                    $scope.scoll.busy = false;
                }
            });
            $scope.filter = {};
            ngDialog.close(window_id);
        };
        //重置查询状态
        $scope.clear = function () {
            $scope.filter = {};
        };
        //打开详情页面
        $scope.show_detail = function (record_code) {
//        $scope.detail_switch = 1;
            ReceiptRecord.get_one_local(record_code).then(function (response) {
                ShopSearch(response.record[0].shop_code).then(function (n) {
                    response.record[0].ShopName = n[0].ShopName;
                });

                response.payment.forEach(function (ret, i) {
                    checkGetList.get_name_by_code(ret.trade_type_code).then(function (n) {
                        response.payment[i].trade_type_name = n[0].CheckName;
                    });
                    var remark = ret.remark.split("||");
                    response.payment[i].reason = remark[1];
                    response.payment[i].remark = remark[0];
                });



                $scope.xiaopiao = {};
                $scope.xiaopiao.record = response.record[0];
                $scope.xiaopiao.record.paymoney = $scope.xiaopiao.record.pay_money > $scope.xiaopiao.record.final_money ? $scope.xiaopiao.record.final_money : $scope.xiaopiao.record.pay_money;
                $scope.xiaopiao.record_detail = response.record_detail;
                $scope.xiaopiao.record.guide_user_name = commonInterface.get_guides_user_name($scope.xiaopiao.record_detail);
                
                $scope.xiaopiao.record.remark = $scope.xiaopiao.record.remark;
                $scope.xiaopiao.payment = response.payment;
                $scope.xiaopiao.promotion = response.promotion;
                switch ($routeParams.cate) {
                    case 'offline':
                        $scope.buttonClose = '1';
                        //added by lqx maowei on 20170406 离线小票不显示补印按钮
                        //$scope.buttonPrint = $rootScope.purview['\/OrderXiaopiaoListCtrl\/to_print:offline'].show;
                        if($scope.xiaopiao.record.is_checkout == 0){
                            $scope.buttonPrint = '0';
                        }else{
                            $scope.buttonPrint = '1';
                        }
                        $scope.buttonReturn = '0';
                        $scope.buttonUpload = '0';
                        $scope.buttonDestroy = '0';
                        $scope.buttonHigh = '0';
                        $scope.buttonRefresh = '0';
                        $scope.buttonThirdReturn = '0';
                        $scope.buttonThirdUpload = '0';
                        //根据 小票单号查询 第三方支付信息
						if($scope.xiaopiao.record.is_checkout == 0){
							third_pay_query();
						}
                        $scope.coupon_display = '0';
                        break;
                    case 'not_upload':
                        $scope.buttonClose = '1';
                        $scope.buttonPrint = '1';
                        $scope.buttonReturn = '0';
                        $scope.buttonUpload = '1';
                        $scope.buttonDestroy = '0';
                        $scope.buttonHigh = '0';
                        $scope.buttonRefresh = '0';
                        $scope.buttonThirdReturn = '0';
                        $scope.buttonThirdUpload = '0';
                        $scope.coupon_display = '0';
                        break;
                }
            });
        };
        //获取支付信息
        var get_third_pay = function(data){
            var pid = $scope.xiaopiao.record_detail[0].pid;
            dpayment = [];
            switch(data.type){
                case '1':{ //微信
                    var info = {
                        'coupon_code':data.transaction_id,
                        'lastchanged':timeStamp2String(0,'datetime'),
                        'money':data.cash_fee/100,
                        'pid':pid,
                        'remark':'交易流水号:'+data.transaction_id,
                        'trade_type_code':data.trade_type == "MICROPAY"?'010':'008',
                        'trade_type_name':data.trade_type == "MICROPAY"?'微信刷卡':'微信扫码'
                    };
                    $scope.xiaopiao.payment.push(info);
                    dpayment.push(info);
                    $scope.xiaopiao.record.pay_money = $scope.xiaopiao.record.paymoney+data.cash_fee/100*1;
                    break;
                }
                case '2':{ //支付宝
                    var info = {
                        'coupon_code':data.trade_no,
                        'lastchanged':timeStamp2String(0,'datetime'),
                        'money':data.total_amount,
                        'pid':pid,
                        'remark':'交易流水号:'+data.trade_no,
                        'trade_type_code':'011',
                        'trade_type_name':'支付宝'
                    };
                    $scope.xiaopiao.payment.push(info);
                    dpayment.push(info);
                    $scope.xiaopiao.record.pay_money = $scope.xiaopiao.record.paymoney+data.total_amount*1;
                    break;
                }
            }
        };
        //查询第三方支付信息
        var third_pay_query = function(){
            var len = $scope.xiaopiao.record_detail.length;
            if(len <= 0){
                return;
            }
            ReceiptRecord.third_pay_query({'record_code':$scope.xiaopiao.record.record_code}).then(function(payment){
                if(payment.data.status == 1){ //获取成功
                    $scope.buttonThirdUpload = 1;
                    switch(payment.data.data.type){
                        case '1':{ //微信
                            get_third_pay(payment.data.data);
                            break;
                        }
                        case '2':{ //支付宝
                            get_third_pay(payment.data.data);
                            break;
                        }
                    }
                } 
            });
        };
        
        //检查是否可以上传小票
        var check_record_upload = function(){
            var paymoney = 0;
            $scope.xiaopiao.payment.forEach(function(e){
                paymoney += e.money*1;
            });
            if(paymoney != $scope.xiaopiao.record.final_money){
                return false;
            }
            return true;
        };
        
        //第三方支付 查询到支付信息 上传
        $scope.goto_third_upload = function(){
            var record_code = $scope.xiaopiao.record.record_code;
            $scope.xiaopiao.record_code = record_code;
            $scope.xiaopiao.record.pay_money = $scope.xiaopiao.record.final_money;
            var data = {};
            data.record_code = record_code;
            data.promotion = $scope.xiaopiao.promotion;
            data.record = $scope.xiaopiao.record;
            data.record_detail = $scope.xiaopiao.record_detail;
            data.payment = dpayment;
            if(check_record_upload()){
                $scope.xiaopiao.record.is_checkout = 1; 
                apiService('pos/pos/update', data).then(function (success,err) {
                    //console.log(success);
                    //console.log(err);
                    if (success.data.status == 1) {
                        baison_app.api.toast({msg:$rootScope.language.TIPS_OPTION_SUCCESS});
                        //更新本地状态为已同步
                        dbUpdate('PosReceiptRecord', {'is_upload': 1,'is_checkout':1}, {record_code: record_code});
                        $scope.buttonThirdUpload = '0';
                    }else{
                        baison_app.api.toast({msg:success.data.message});
                    }
                    
                });
            }else{
                baison_app.api.toast({msg:$rootScope.language.TIP_NO_PAYED});
            }
            
        };
//关闭详情
        $scope.close_detail = function () {
//        $scope.detail_switch = 0;
            $scope.buttonFilter();
        };
        //上传离线数据
        $scope.btn_upload = function (ret) {
            $scope.xiaopiao_list = [];
            var data = [];
            data.push(ret.record);
            waitUpLoad.receipt_record_loacl_to_api(data).then(function (ret) {
                baison_app.api.toast({msg: $rootScope.language.TIPS_CUNHUO_CONFIRM_SUCCESS});
            });

            setTimeout(function () {
                $scope.close_detail();
                $scope.refresh_data();

            }, 1000);
        };

        $scope.to_print = function () {
            //正式版
            //获取小票商品明细信息
            var get_goods_detail_infos = function(record_detail){
                var deferred = $q.defer();
                var promise = deferred.promise;
                record_detail.forEach(function(n,i){
                    deferred[i] = $q.defer();
                    promise[i] = deferred[i].promise;
                    dbGetOne('select weight,unit_name from PosGoods where GoodsCode="' + n.goods_code + '" order by CategoryCode desc, SortCode desc').then(function(ret){
                        
                        n.weight = ret.weight;
                        n.unit_name = ret.unit_name;
                        deferred[i].resolve('success');
                    });
                   
                });
                return $q.all(promise);
            };
            //获取小票信息
            ReceiptRecord.get_one_local($scope.xiaopiao.record.record_code).then(function (ret) {
                var record = {};
                var configData = $api.getStorage('configData');
                var printData = configData.printData;
                var shop_addr = '';
                var shop_tel = '';
                var web_site = '';
                if (printData != undefined) {
                    shop_addr = printData['addr'];
                    shop_tel = printData['tel'];
                    web_site = printData['web_site'];
                }
                var shop_name = '';
                ShopSearch(ret.record[0].shop_code).then(function (n) {
                    shop_name = n[0].ShopName;
                    record.shop_name = shop_name;
                    record.record_code = ret.record[0].record_code;
                    record.record_time = ret.record[0].record_time;
                    record.vip_code = ret.record[0].vip_code;
                    record.pay_money = ret.record[0].final_money;
                    record.total_num = ret.record[0].num;
                    record.total_money = ret.record[0].money;
                    record.user_code = ret.record[0].trade_user_code;
                    record.user_name = ret.record[0].trade_user_code;
                    record.brand_name = $rootScope.language.TIP_ORDER_RECEIPT_BRAND_NAME;
                    record.shop_addr = shop_addr;
                    record.shop_tel = shop_tel;
                    record.web_site = web_site;
                    record.record_detail = ret.record_detail;
                    record.guide_user_name = commonInterface.get_guides_user_name(ret.record_detail);//导购员
                    record.payment = ret.payment;
                    record.promotion = ret.promotion;
                    get_goods_detail_infos(record.record_detail).then(function(){
                        Print.xp_print($rootScope.language.TIP_ORDER_RECEIPT_BRAND_PRINT, record);
                    });
                    
                });
            });
           
        };

    }];

//网单部分 #####################################################################

//服务单部分 #####################################################################
//商品定金单
var OrderServiceDepositRecordList = ['$scope', '$rootScope', '$location','ShopSetCheck','ServiceOrder','$routeParams','timeStamp2String','ngDialog',function($scope,$rootScope,$location,ShopSetCheck,ServiceOrder,$routeParams,timeStamp2String,ngDialog){
	//检查店铺是否绑定
	ShopSetCheck();  
    $scope.curr = "DepositRecord_list";
    var window_id = 0;
    //定金单的状态
    $scope.statusData = [];
	//从服务器搜索
	var search_api = function(filter) {
		ServiceOrder.get_depositRecord(filter).then(function(ret) {
//		    baison_app.api.alert({title:'定金单',msg:ret});
		    if(ret.data.status == 1){
		         $scope.is_show = "true";
		    	 var data = [];
		    	 $.each(ret.data.data,function(i,n){
		    	      	 data.push({
			    	 	  	key : i,
			    	 	  	value : n
			    	 	  });
		    	 });
		    	 $scope.order_list = data;
		    }else if(ret.data.status != 1){
		        $scope.is_show = "";
		    	baison_app.api.alert({
					buttons : [$rootScope.language.COMMON_CONFIRM_BUTTON],
					title : $rootScope.language.COMMON_TITLE_HINT,
					msg : ret.data.message
				});
				return;
		    }
		}).catch(function(err) {
			baison_app.api.toast({
				msg : $rootScope.language.MSG_NOT_CONNECTED_INTERNET
			});
		});
	};
	//默认查找在线（服务器搜索）
	search_api({
		shop_code : $rootScope.current_shop.ShopCode
//		day : timeStamp2String(new Date(), 'datetime')
	});
	//滑动事件加载锁========================================
	//往下点击获取更多
	$scope.scoll = {};
	//滚动加载器
	$scope.scoll.busy = false;
	$scope.scoll.nextpage = function() {
		if ($scope.scoll.busy){
			return;
		}
			
		baison_app.api.toast({
			msg : $rootScope.language.COMMON_LOADING
		});
		$scope.scoll.busy = true;
		var param = {};
		param.shop_code = $rootScope.current_shop.ShopCode;
		param.is_pre = 1;
		if ($scope.order_list.length > 0) {
			param.day = $scope.order_list[$scope.order_list.length - 1]['key'];
		}
		//获取最近日期的单据
		ServiceOrder.get_depositRecord(param).then(function(ret) {
			if (ret.data.status == 1) {
			     $.each(ret.data.data,function(i,n){
		    	      	 $scope.order_list.push({
			    	 	  	key : i,
			    	 	  	value : n
			    	 	  });
		    	 });
				$scope.scoll.busy = false;
			} else if (ret.data.status == -1) {
				baison_app.api.toast({
					msg : $rootScope.language.TIPS_CUNHUO_MESSAGE
				});
				$scope.is_show = "";
			}
		});
	};
    //查询操作
    $scope.search = function(){
         //条件：单据编号、起始时间、结束时间、code状态、经手店员、手机号
    	  $scope.filter = {};
    	  $scope.filter.record_code = '';
    	  $scope.filter.record_time_start = '';
    	  $scope.filter.record_time_end = '';
    	  $scope.filter.code = '';
    	  $scope.filter.userCode = '';
    	  $scope.filter.tel = '';
    	  window_id = ngDialog.open({
                trapFocus: false,
                template: './html/order/service/deposit_select.html',
                disableAnimation: true,
                scope: $scope,
                //                closeByDocument: false
            });
    };
    
   //定金单的状态：
    $scope.statusData = [{
    	'name':$rootScope.language.TIP_CONFIRMED,code:1  //已确认
    },{
    	'name':$rootScope.language.TIP_USED,code:2  //已使用
    },{
    	'name':$rootScope.language.TIP_RETURN,code:3 //  已退款
    }];
   //弹框操作+++++++++++++++++++++++++++++++++++++++++++++++++++++++
   //重置
   $scope.clear = function(){
   	  $scope.filter = {};
   };
   //确定
   $scope.search_data = function(data){
       //根据条件查询商品定金单的信息
   	   ServiceOrder.search_depositRecord_by_cond(data).then(function(ret){
   	   		if(ret.data.status == 1){
		         $scope.is_show = "true";
		    	 var data = [];
		    	 $.each(ret.data.data,function(i,n){
		    	      	 data.push({
			    	 	  	key : i,
			    	 	  	value : n
			    	 	  });
		    	 });
		    	 $scope.order_list = data;
		    }else if(ret.data.status == -1){
		        $scope.is_show = "";
		    	baison_app.api.alert({
					buttons : [$rootScope.language.COMMON_CONFIRM_BUTTON],
					title : $rootScope.language.COMMON_TITLE_HINT,
					msg : ret.data.message
				});
				return;
		    }
		    ngDialog.close(window_id);
   	   });
   };
}];
//获取商品订金单详细信息
var OrderServiceDepositRecordDetail = ['$scope', '$rootScope', '$location','ShopSetCheck','ServiceOrder','$routeParams','ngDialog','timeStamp2String',function($scope,$rootScope,$location,ShopSetCheck,ServiceOrder,$routeParams,ngDialog,timeStamp2String){
	//检查店铺是否绑定
	ShopSetCheck();
	$scope.curr = "DepositRecord_list";
	var filter = {};
    filter.record_code = $routeParams.record_code;
	var window_id = 0;
	//商品明细、结算明细、流水明细、附加明细
	$scope.gDetail = "true";
	$scope.sDetail = "";
	$scope.lDetail = "";
	$scope.aDetail = "";
	//获取详细信息
	ServiceOrder.get_record_detail_by_record_code(filter).then(function(response){
	   //这个详细信息：需要有商品明细、结算明细、流水明细和附加明细
	 	if(response.data.status == 1){
	 		$scope.DepositDetail = response.data.data;
			//已确认
			if($scope.DepositDetail.record.state == 1){
				$scope.is_return = 'true';
			}else if($scope.DepositDetail.record.state == 3){ //已退款
				$scope.is_return = '';
			}else{
				$scope.is_return = '';
			}
	 	}else{
	 		baison_app.api.toast({msg:response.data.message});
	 	}
	});
	//私有方法+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
	//订金退款
	var deposit_return = function(moneyData){
//	 	baison_app.api.alert({title:'订金退款',msg:moneyData});
		$scope.moneyData = moneyData;
		window_id = ngDialog.open({
                trapFocus: false,
                template: './html/order/service/dialog/deposit_money.html',
                disableAnimation: true,
                scope: $scope,
                controller: ['$scope', function ($scope) {
                        //订金退款确认
						 $scope.confirm_deposit_money = function(record){ //包含表头信息
//						  
						 	 	//生成订金退款单
							 	var param = {};
	                        	param.tel = record.phone;
	                        	param.customer_code = record.customer_code; //顾客代码
	                        	param.UserCode = $rootScope.gkdjd_userCode;  //经手店员：订金单的经手店员
	                        	param.shop_code = record.shop_code;  //店铺代码
	                        	param.date = timeStamp2String(new Date(), 'datetime');    //日期
	                        	param.record_code = record.record_code; //订金单号
	                        	param.balance_money = record.balance_money;           //订金余额
            					param.return_money = record.balance_money;  //退款金额
            					param.is_made_person = $rootScope.user.UserName;         //制单人
                              	param.is_made_time = timeStamp2String(new Date(), 'datetime');     //制单日期
                                param.is_sure_person = $rootScope.user.UserName;   //确认人
            					param.is_sure_time =  timeStamp2String(new Date(), 'datetime');              //确认日期
            					param.address=record.address;   //地址
	                        	//传结算方式和名称
	                        	param.check_code = '000';//现金退款
	                        	param.return_money = record.balance_money;//退款金额
	                         ServiceOrder.add_depositReturnRecord(param).then(function(ret){
	                        	 if(ret.data.status == 1){
	                        	 	 ngDialog.close(window_id);
	                        	 	 baison_app.api.toast({msg:ret.data.message});
	                        	 	 $location.path('/order/service/depositRecord_list');
	                        	 }else{
	                        	 	baison_app.api.toast({msg:ret.data.message});
	                        	 }
	                         });
						 };
						 
						 //订金退款取消
						 $scope.cancel_deposit_money = function(){
						    //返回订金单详情
						 	ngDialog.close(window_id);
						 };
                    }]
            });
	};
	 //===============================================================
	 //商品明细
	 $scope.item = function(){
	 	$scope.gDetail = "true";
		$scope.sDetail = "";
		$scope.lDetail = "";
		$scope.aDetail = "";
	 };
	 //结算方式
	 $scope.settle = function(){
	 	$scope.gDetail = "";
		$scope.sDetail = "true";
		$scope.lDetail = "";
		$scope.aDetail = "";
	 };
	 //流水明细
	 $scope.ls = function(){
	 	$scope.gDetail = "";
		$scope.sDetail = "";
		$scope.lDetail = "true";
		$scope.aDetail = "";
	 };
	 //附加明细
	 $scope.addition = function(){
	 	$scope.gDetail = "";
		$scope.sDetail = "";
		$scope.lDetail = "";
		$scope.aDetail = "true";
	 };
	 //表头操作：退款和关闭操作++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	 //退款
	 $scope.return_money = function(record){
	    //订金主表信息
	    $scope.record = record;
	 	window_id = ngDialog.open({
                trapFocus: false,
                template: './html/order/service/dialog/return_money.html',
                disableAnimation: true,
                scope: $scope,
                controller: ['$scope', function ($scope) {
                        //确认删除商品
                        $scope.confirm_return_money = function (record) {
                            ngDialog.close(window_id);                           
                            //做退款操作
                            deposit_return(record);
                        };
                        //取消删除商品
                        $scope.cancel_return_money = function () {
                            ngDialog.close(window_id);
                        };
                    }]
            });
	 };
	 
	 //关闭
	 $scope.come_back = function(){
	 	$location.path('/order/service/depositRecord_list');
	 };
	 
	 //弹窗处理+++++++++++++++++++++++++++++++++++++++++++++++++++
	 
}];
//订金退款单
var OrderServiceDepositReturnRecord = ['$scope', '$rootScope', '$location','ShopSetCheck','ServiceOrder','$routeParams','ngDialog','timeStamp2String',function($scope,$rootScope,$location,ShopSetCheck,ServiceOrder,$routeParams,ngDialog,timeStamp2String){
	//检查店铺是否绑定
	ShopSetCheck();
	$scope.curr = "DepositReturnRecord_list";
	
	//来源类型：
    $scope.statusData = [{
    	'name':$rootScope.language.TIPS_DEPOSIT_RETURN,code:0   // 定金单直接退款
    },{
    	'name':$rootScope.language.TIPS_DEPOSIT_RETURN_MONEY_AFTER_USE,code:1  //订金单在前台使用后退余额
    },{
    	'name':$rootScope.language.TIPS_USE_DEPOSIT,code:2  //使用订金
    }];
    
    //从服务器搜索,获取订金退款单
	var search_api = function(filter) {
		ServiceOrder.get_depositReturnRecord(filter).then(function(ret) {
//		    baison_app.api.alert({title:'定金退款单',msg:ret});
		    if(ret.data.status == 1){
		         $scope.is_show = "true";
		    	 var data = [];
		    	 $.each(ret.data.data,function(i,n){
		    	      	 data.push({
			    	 	  	key : i,
			    	 	  	value : n
			    	 	  });
		    	 });
		    	 $scope.orderReturn_list = data;
		    }else if(ret.data.status != 1){
		        $scope.is_show = "";
		    	baison_app.api.alert({
					buttons : [$rootScope.language.COMMON_CONFIRM_BUTTON],
					title : $rootScope.language.COMMON_TITLE_HINT,
					msg : ret.data.message
				});
				return;
		    }
		}).catch(function(err) {
			baison_app.api.toast({
				msg : $rootScope.language.MSG_NOT_CONNECTED_INTERNET
			});
		});
	};
	//默认查找在线（服务器搜索）
	search_api({
		shop_code : $rootScope.current_shop.ShopCode
	});
	
	//滑动事件加载锁========================================
	//往下点击获取更多
	$scope.scoll = {};
	//滚动加载器
	$scope.scoll.busy = false;
	$scope.scoll.nextpage = function() {
		if ($scope.scoll.busy){
			return;
		}		
		baison_app.api.toast({
			msg : $rootScope.language.COMMON_LOADING
		});
		$scope.scoll.busy = true;
		var param = {};
		param.is_pre = 1;
		param.shop_code = $rootScope.current_shop.ShopCode;
		if ($scope.orderReturn_list.length > 0) {
			param.day = $scope.orderReturn_list[$scope.orderReturn_list.length - 1]['key'];
		}
		//获取最近日期的单据
		ServiceOrder.get_depositReturnRecord(param).then(function(ret) {
			if (ret.data.status == 1) {
			     $.each(ret.data.data,function(i,n){
		    	      	 $scope.orderReturn_list.push({
			    	 	  	key : i,
			    	 	  	value : n
			    	 	  });
		    	 });
				$scope.scoll.busy = false;
			} else if (ret.data.status == -1) {
				baison_app.api.toast({
					msg : $rootScope.language.TIPS_CUNHUO_MESSAGE
				});
				$scope.is_show = "";
			}
		});
	};
	
	
	//查询操作
    $scope.search = function(){
         //条件：单据编号、起始时间、结束时间、来源类型、经手店员、手机号
    	  $scope.filter = {};
    	  $scope.filter.record_code = '';
    	  $scope.filter.record_time_start = '';
    	  $scope.filter.record_time_end = '';
    	  $scope.filter.code = '';
    	  $scope.filter.userCode = '';
    	  $scope.filter.tel = '';
    	  window_id = ngDialog.open({
                trapFocus: false,
                template: './html/order/service/depositReturn_select.html',
                disableAnimation: true,
                scope: $scope,
                //                closeByDocument: false
            });
    };
	
	
	
	//弹窗++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	$scope.search_data = function(data){
		//根据条件查询商品定金退款单单据
   	   ServiceOrder.search_depositReturnRecord_by_cond(data).then(function(ret){
   	   		if(ret.data.status == 1){
		         $scope.is_show = "true";
		    	 var data = [];
		    	 $.each(ret.data.data,function(i,n){
		    	      	 data.push({
			    	 	  	key : i,
			    	 	  	value : n
			    	 	  });
		    	 });
		    	 $scope.orderReturn_list = data;
		    }else if(ret.data.status == -1){
		        $scope.is_show = "";
		    	baison_app.api.alert({
					buttons : [$rootScope.language.COMMON_CONFIRM_BUTTON],
					title : $rootScope.language.COMMON_TITLE_HINT,
					msg : ret.data.message
				});
				return;
		    }
		    ngDialog.close(window_id);
   	   });
	};
	//关闭
	$scope.clear = function(){
		 $scope.filter = {};
	};
}];
//定金退款单详情
var OrderServiceDepositReturnRecordDetail = ['$scope', '$rootScope', '$location','ShopSetCheck','ServiceOrder','$routeParams','ngDialog','timeStamp2String',function($scope,$rootScope,$location,ShopSetCheck,ServiceOrder,$routeParams,ngDialog,timeStamp2String){
	//检查店铺是否绑定
	ShopSetCheck();
	$scope.curr = "DepositReturnRecord_list";
	var filter = {};
    filter.record_code = $routeParams.record_code;
	var window_id = 0;
	
	//获取详细信息
	ServiceOrder.get_returnRecord_detail_by_record_code(filter).then(function(response){
	   //这个详细信息：结算明细、附加明细
	 	if(response.data.status == 1){
	 		$scope.DepositReturnDetail = response.data.data;
	 	}else{
	 		baison_app.api.toast({msg:response.data.message});
	 	}
	});
	//结算明细、附加明细
	$scope.sDetail = "true";
	$scope.aDetail = "";
	//结算方式
	 $scope.settle = function(){
		$scope.sDetail = "true";
		$scope.aDetail = "";
	 };
	 //附加明细
	 $scope.addition = function(){
		$scope.sDetail = "";
		$scope.aDetail = "true";
	 };
	 
	 //关闭
	 $scope.come_back = function(){
	 	$location.path('/order/service/depositReturnRecord_list');
	 };
	 
	 
	
}];

//维修单>>>>
var OrderFixListCtrl = ['$scope', '$rootScope', '$location', 'timeStamp2String', '$routeParams', 'ngDialog', 'ServiceOrder', 'ShopSetCheck', 'Print', 'ShopSearch', 'checkGetList', function ($scope, $rootScope, $location, timeStamp2String, $routeParams, ngDialog, ServiceOrder, ShopSetCheck, Print, ShopSearch, checkGetList) {
        ShopSetCheck();
        var record_list_buntton = function () {
            $scope.buttonHigh = 1;
            $scope.buttonRefresh = 1;
            $scope.buttonRepair = 0;
            $scope.buttonStop = 0;
            $scope.buttonClose = 0;
            $scope.buttonRefresh = 1;
            $scope.buttonMore = 1;
            $scope.buttonOption = 0;
        };
        record_list_buntton();
        var record_detail_buntton = function () {
            $scope.buttonHigh = 0;
            $scope.buttonRefresh = 0;
            $scope.buttonRepair = 1;
//        $scope.buttonStop = 1;
            $scope.buttonClose = 1;
            $scope.buttonRefresh = 0;
        };

        $scope.curr = "service_list_fix";
        var now_date = new Date();
        $scope.refresh_data = function () {
            $scope.fix_list = [];
            var filter = {};
            filter.date = now_date;
            ServiceOrder.get_fixRecord(filter).then(function (res) {
                //baison_app.api.alert({msg:res});
                if (res.data.status == 1) {
                    for (var i in res.data.data) {
                        $scope.fix_list.push({key: i, value: res.data.data[i]});
                    }
                }
            });
        };
        $scope.refresh_data();

        $scope.scoll = {}; //滚动加载器
        $scope.scoll.busy = false;
        $scope.scoll.nextpage = function () {
            if ($scope.scoll.busy) {
                return;
            }
            //baison_app.api.toast({msg: '异步加载...'});
            $scope.scoll.busy = true;
            var filter = {};
            filter.is_pre = 1;
            if ($scope.fix_list.length > 0) {
                filter.date = $scope.fix_list[$scope.fix_list.length - 1]['key'];
            } else {
                filter.date = now_date;
            }
            ServiceOrder.get_fixRecord(filter).then(function (res) {
                if (res.data.status == 1) {
                    for (var i in res.data.data) {
                        $scope.fix_list.push({key: i, value: res.data.data[i]});
                    }
                } else {
                    baison_app.api.toast({msg: $rootScope.language.TIP_ORDER_RECEIPT_DATA});
                }
                $scope.scoll.busy = false;
            });
        };

        $scope.show_detail = function (record_code) {
            $scope.fix = {};
            record_detail_buntton();
            ServiceOrder.get_fixDetail(record_code).then(function (res) {
                //baison_app.api.alert({msg:res});
                if (res.data.status == 1) {
                    $scope.fix.record = res.data.data.record[0];
                    $scope.fix.record.ShopName = $rootScope.current_shop.ShopName;
                    $scope.fix.detail = res.data.data.detail;
                    $scope.fix.payment = res.data.data.payment;
                    ShopSearch($scope.fix.record.shop_code).then(function (n) {
                        n.forEach(function (v) {
                            if (v.ShopCode == $scope.fix.record.shop_code) {
                                $scope.fix.record.ShopName = v.ShopName;
                            }
                        });
                    });
                    if ($scope.fix.record.record_status >= 5) {
                        $scope.buttonStop = 0;
                    } else {
                        $scope.buttonStop = 1;
                    }
                    if ($scope.fix.record.record_status >= 4) {
                        $scope.buttonOption = 0;
                    } else {
                        $scope.buttonOption = 1;
                        $scope.option_code = parseInt($scope.fix.record.record_status) + 1;
                    }
                    if ($scope.fix.record.record_status == 4) {
                        $scope.buttonOption = 1;
                        $scope.option_code = 6;
                    }
                    //baison_app.api.alert({msg:$scope.option_code});
                }
            });

        };
        var window_id = 0;
        $scope.search = function () {
            $scope.filter = {};
            window_id = ngDialog.open({closeByDocument: false,
                template: './html/order/service/dialog/search.html',
                disableAnimation: true,
                scope: $scope,
                trapFocus: false
            });
        };
        $scope.option = function () {
            window_id = ngDialog.open({closeByDocument: false,
                template: './html/order/service/dialog/option.html',
                disableAnimation: true,
                scope: $scope,
                trapFocus: false
            });
        };
        $scope.stop = function () {
            window_id = ngDialog.open({closeByDocument: false,
                template: './html/order/service/dialog/stop.html',
                disableAnimation: true,
                scope: $scope,
                trapFocus: false
            });
        };


        $scope.fix_option = function (status, fix_describe) {
            if (!$scope.fix.record.record_code) {
                return;
            }
            var data = {
                option_code: status,
                record_code: $scope.fix.record.record_code,
                describe: fix_describe,
                person: $rootScope.user.UserName
            };
            ServiceOrder.update_fixRecord(data).then(function (res) {
                if (res.data.status == 1) {
                    baison_app.api.toast({msg: $rootScope.language.TIPS_OPTION_SUCCESS});
                    $scope.show_detail($scope.fix.record.record_code);
                }
                //baison_app.api.alert({msg:res,title:"update_wxd"})
            });
            ngDialog.close(window_id);
        };

        $scope.cancel = function () {
            ngDialog.close(window_id);
        };
        $scope.search_data = function (filter) {
            if ($scope.filter.record_time_start == '' || $scope.filter.record_time_end == '' ||
                    typeof $scope.filter.record_time_start == 'undefined' ||
                    typeof $scope.filter.record_time_end == 'undefined') {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIPS_CUNHUO_MESSAGE_TIME});
                return;
            }
            if ($scope.filter.record_time_start > $scope.filter.record_time_end) {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIPS_CUNHUO_MESSAGE_TIME_LIMIT});
                return;
            }
            ServiceOrder.search_fixRecord(filter).then(function (res) {
                $scope.fix_list = [];
                //baison_app.api.alert({msg:res});
                if (res.data.status == 1) {
                    for (var i in res.data.data) {
                        $scope.fix_list.push({key: i, value: res.data.data[i]});
                    }
                }
                $scope.scoll.busy = false;
            });
            ngDialog.close(window_id);
        };
        $scope.clear = function () {
            $scope.filter = {};
        };
        $scope.close_detail = function () {
            record_list_buntton();
            $scope.refresh_data();
        }
    }];
var OrderFixDetailCtrl = ['$scope', '$rootScope', '$location', 'timeStamp2String', '$routeParams', 'ngDialog', 'ServiceOrder', 'ShopSetCheck', 'Print', 'ShopSearch', 'checkGetList', function ($scope, $rootScope, $location, timeStamp2String, $routeParams, ngDialog, ServiceOrder, ShopSetCheck, Print, ShopSearch, checkGetList) {
        ShopSetCheck();
    }];


//关联
OrderNavCtrl.$injector = ['$scope', '$rootScope','ThirdDev'];
OrderXiaopiaoListCtrl.$injector = ['$q', '$scope', '$rootScope', '$location','apiService','dbUpdate', 'timeStamp2String', '$routeParams', 'ngDialog', 'ReceiptRecord', 'ShopSetCheck', 'Print', 'ShopSearch', 'dbGetOne', 'checkGetList','commonInterface','PosUser'];
OrderXiaopiaoDetailCtrl.$injector = ['$scope', '$routeParams', 'ReceiptRecord'];
OrderXiaopiaoListLocalCtrl.$injector = ['$q','$scope', '$rootScope', '$location', 'timeStamp2String', '$routeParams', 'ngDialog', 'BaseGoods', 'ReceiptRecord', 'ShopSetCheck', 'Print', 'waitUpLoad', 'ShopSearch', 'checkGetList','dbGetOne','apiService','dbUpdate','commonInterface'];
OrderServiceDepositRecordList.$injector =['$scope', '$rootScope', '$location','ShopSetCheck','ServiceOrder','$routeParams','timeStamp2String','ngDialog'];
OrderServiceDepositRecordDetail.$injector =['$scope', '$rootScope', '$location','ShopSetCheck','ServiceOrder','$routeParams','ngDialog','timeStamp2String'];
OrderServiceDepositReturnRecord.$injector=['$scope', '$rootScope', '$location','ShopSetCheck','ServiceOrder','$routeParams','ngDialog','timeStamp2String'];
OrderServiceDepositReturnRecordDetail.$injector = ['$scope', '$rootScope', '$location','ShopSetCheck','ServiceOrder','$routeParams','ngDialog','timeStamp2String'];
OrderFixListCtrl.$injector = ['$scope', '$rootScope', '$location', 'timeStamp2String', '$routeParams', 'ngDialog', 'ServiceOrder', 'ShopSetCheck', 'Print', 'ShopSearch', 'checkGetList'];
OrderFixDetailCtrl.$injector = ['$scope', '$rootScope', '$location', 'timeStamp2String', '$routeParams', 'ngDialog', 'ServiceOrder', 'ShopSetCheck', 'Print', 'ShopSearch', 'checkGetList'];
